<?php
	mysql_connect("127.0.0.1", "root", "qwerty"); 
	mysql_select_db('test'); 
	mysql_set_charset('UTF-8'); 
	date_default_timezone_set('Europe/Moscow'); 
?>
<html>
	<head>
		<title>Пользователи анаграммы</title>
		<style>
			body {
				
			}
			
			a { text-decoration: none; color: #069 }
			a:hover { text-decoration: underline }
			
			table {
				background-color: #ffffff; 
				border: 1px solid #666; 
				border-collapse: collapse; 
				width: 100%; 
			}
			
			td {
				background-color: #fafafa; 
				border: 1px solid #666; 
				padding: 2px; 
			}
			
			tr {
				border: 1px solid #666; 
			}
			
			th {
				background-color: #f0f0f0; 
				border: 1px solid #666; 
				padding: 2px; 
			}
		</style>
	</head>
	
	<body>
<?php		
	$sort = isset($_GET['sort']) ? $_GET['sort'] : 'day'; 
	$sql = ''; 
	switch ($sort) {
		case "day":
			$time = localtime(time()); 
			$start = mktime(0, 0, 0, $time[4] + 1, $time[3] - $offsets[$time[6]], 1900 + $time[5]); 
			$sql = "WHERE `last_answer` >= ".$start." ORDER BY `total_day` DESC, `balls_day` DESC"; 
			$balls_key = 'balls_day'; 
			$answers_key = 'total_day'; 
		break; 
		
		case "week":
			$time = localtime(time()); 
			$offsets = array(6, 0, 1, 2, 3, 4, 5); 
			$start = mktime(0, 0, 0, $time[4] + 1, $time[3] - $offsets[$time[6]], 1900 + $time[5]); 
			$sql = "WHERE `last_answer` >= ".$start." ORDER BY `total_week` DESC, `balls_week` DESC"; 
			$balls_key = 'balls_week'; 
			$answers_key = 'total_week'; 
		break; 
		
		default:
			$sql = "ORDER BY `total` DESC, `balls` DESC"; 
			$balls_key = 'balls'; 
			$answers_key = 'total'; 
		break; 
	}
	
	$sorts = array (
		"day"  => "За сутки", 
		"week" => "За неделю", 
		"all"  => "За всё время", 
	); 
	
	$last_element = end(array_keys($sorts)); 
	foreach ($sorts as $key => $sort_name) {
		if ($sort == $key)
			echo '<b>'.$sort_name.'</b>'; 
		else
			echo '<a href="?sort='.$key.'">'.$sort_name.'</a>'; 
		if ($key != $last_element)
			echo ' | '; 
	}
	
	echo '<table>'; 
	echo '<tr><th>Игрок</th><th>Ответов</th><th>Баллы</th></tr>'; 
	
	$req = mysql_query("SELECT * FROM `anagram_users` ".$sql); 
	while ($res = mysql_fetch_assoc($req)) {
		echo '<tr>'; 
		echo '<td><a href="http://'.$res['nick'].'.spaces.ru"><b style="color: #79358c">'.$res['nick'].'</b></a></td>'; 
		echo '<td>'.$res[$answers_key].'</td>'; 
		echo '<td>'.$res[$balls_key].'</td>'; 
		echo '</tr>'; 
	}
	echo '</table>'; 
?>
	</body>
</html>
	
